<?php
namespace app\admin\controller;

use \think\facade\Db;
use app\middleware\JWTAUTH;
use app\admin\model\accounttype;
use app\admin\model\Procount;
use app\admin\model\Proplan;
use app\admin\model\Page;
use app\admin\model\Url;
use app\admin\model\Visitor;
use app\admin\model\Blacklistip;
use think\exception\ValidateException;

class Visitor
{
    protected $middleware=['JWTAUTH'];

    public function create(){
      if($this->request->action()=='post'){

          try{
            $visitor_data=array(
                'ip' => $this->request->param('ip'),
                'token' => $this->request->param('token'),
                'time' => time(),
            );

            $this->validate(app\validate\Visitor::class)
                  ->scene('create')
                  ->check($visitor_data);
            $visitor=new Visitor;
            $visitor->save($visitor_data);

          }catch(ValidateException $e){
              throw($e);
          }
      }else{
          return json_encode(array(
              'status' => '103',
              'message' => '方法不支持，请用post',
          ));
      }
    }

    public function edit()
    {
      if($this->request->action()=='post'){

          $visitor_own_user_id=$token_user_id; //从token中取出

          try{
            $id=$this->request->param('id');
            $visitor_data=array(
              'ip' => $this->request->param('ip'),
              'token' => $this->request->param('token'),
              'time' => time(),
            );

            $this->validate(app\validate\Visitor::class)
                  ->scene('edit')
                  ->check($visitor_data);
            $visitor=Url::find(array('id'=>$id,'owned_user_id' => $visitor_own_user_id)); //注意proaccount_id

            if(empty($visitor)){
                return json_encode(array(
                    'status' => '201',
                    'message' => '没有该推广页面',
                ));
            }
            $visitor->save($visitor_data);

          }catch(ValidateException $e){
              throw($e);
          }
      }else {
          return json_encode(
            array(
              'status' => '103',
              'message' =>'方法错误',
            )
          );
      }
    }


    public function delete(){
        $visitor_id=$this->request->param('id');
        $visitor_own_user_id=$token_user_id; //从token中取出
        try{
            $this->validate(app\validate\Visitor::class)
                ->scene('delete')
                ->check($visitor_id);

            $visitor=Visitor::find(array(
                'id' => $visitor_id,
                'owned_user_id' => $visitor_own_user_id,
            ));  //注意 proaccount_id

            if($visitor->delete()){
                return json_encode(array(
                    'status' => '100',
                    'message' => '删除成功',
                ));
            }else{
                return json_encode(array(
                    'status' => '203',
                    'message' => '删除失败，请联系管理员查看原因',
                ));
            }
        }catch(ValidateException $e){
            throw($e);
        }
    }

    public findbyname(){
        $visitor_name=$this->request->param('name');
        $visitor_own_user_id=$token_user_id; //从token中取出
        try{
            $this->validate(app\validate\Visitor::class)
                ->scene('findbyname')
                ->check($visitor_name);

            $visitor=Visitor::find(array(
                'name' => $visitor_name,
                'owned_user_id' => $visitor_own_user_id,
            ));

            if(!empty($visitor)){
                return json_encode($visitor);
            }else{
                return json_encode(array(
                    'status' => '303',
                    'message' => '未查找到数据',
                ));
            }
        }catch(ValidateException $e){
            throw($e);
        }
    }
}
